const Koa = require('koa');
const path = require('path');
const serve = require('koa-static');
const views = require('koa-views');
const Router = require('@koa/router');
const router = new Router();
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.set('Access-Control-Allow-Origin', '*');
  ctx.set('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
  ctx.set('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
  ctx.set('Cross-Origin-Embedder-Policy', 'require-corp');
  ctx.set('Cross-Origin-Opener-Policy', 'same-origin');
  if (ctx.method === 'OPTIONS') {
    ctx.body = 200;
  } else {
    await next();
  }
});

app.use(serve('.'));
app.use(serve(path.join(__dirname, './asset')));
app.use(views(path.join(__dirname, './views')));
router.get('/', index);
router.get('/index2', index2);
router.get('/index3', index3);

async function index(ctx) {
  await ctx.render('index');
}
async function index2(ctx) {
  await ctx.render('index2');
}
async function index3(ctx) {
  await ctx.render('index3');
}

app.use(router.routes());
app.listen(3001);
